package cn.iocoder.yudao.module.ai.service.materialreleasefile;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ai.controller.admin.materialreleasefile.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.materialreleasefile.MaterialReleaseFileDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ai.dal.mysql.materialreleasefile.MaterialReleaseFileMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.*;

/**
 * 素材解压缩文件列 Service 实现类
 *
 * @author 侯俊青
 */
@Service
@Validated
public class MaterialReleaseFileServiceImpl implements MaterialReleaseFileService {

    @Resource
    private MaterialReleaseFileMapper materialReleaseFileMapper;

    @Override
    public Long createMaterialReleaseFile(MaterialReleaseFileSaveReqVO createReqVO) {
        // 插入
        MaterialReleaseFileDO materialReleaseFile = BeanUtils.toBean(createReqVO, MaterialReleaseFileDO.class);
        materialReleaseFileMapper.insert(materialReleaseFile);
        // 返回
        return materialReleaseFile.getId();
    }

    @Override
    public void updateMaterialReleaseFile(MaterialReleaseFileSaveReqVO updateReqVO) {
        // 校验存在
        validateMaterialReleaseFileExists(updateReqVO.getId());
        // 更新
        MaterialReleaseFileDO updateObj = BeanUtils.toBean(updateReqVO, MaterialReleaseFileDO.class);
        materialReleaseFileMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaterialReleaseFile(Long id) {
        // 校验存在
        validateMaterialReleaseFileExists(id);
        // 删除
        materialReleaseFileMapper.deleteById(id);
    }

    @Override
    public void deleteByMaterialId(Long materialId) {
        materialReleaseFileMapper.delete(MaterialReleaseFileDO::getMaterialId, materialId);
    }

    private void validateMaterialReleaseFileExists(Long id) {
        if (materialReleaseFileMapper.selectById(id) == null) {
            throw exception(MATERIAL_RELEASE_FILE_NOT_EXISTS);
        }
    }

    @Override
    public MaterialReleaseFileDO getMaterialReleaseFile(Long id) {
        return materialReleaseFileMapper.selectById(id);
    }

    @Override
    public PageResult<MaterialReleaseFileDO> getMaterialReleaseFilePage(MaterialReleaseFilePageReqVO pageReqVO) {
        return materialReleaseFileMapper.selectPage(pageReqVO);
    }

}